<template>
	<view class="ticket-detail-container">
		<view class="ticket-header">
			<text class="ticket-title">{{ ticket.title }}</text>
			<text class="ticket-status" :class="ticket.status">{{ getStatusText(ticket.status) }}</text>
		</view>
		
		<view class="ticket-info">
			<view class="info-item">
				<text class="iconfont icon-id info-icon"></text>
				<text class="label">工单编号：</text>
				<text class="value">{{ ticket.id }}</text>
			</view>
			<view class="info-item">
				<text class="iconfont icon-time info-icon"></text>
				<text class="label">提交时间：</text>
				<text class="value">{{ ticket.createTime }}</text>
			</view>
			<view class="info-item">
				<text class="iconfont icon-priority info-icon"></text>
				<text class="label">问题类型：</text>
				<text class="value">{{ ticket.type }}</text>
			</view>
			<view class="info-item">
				<text class="iconfont icon-status info-icon"></text>
				<text class="label">当前状态：</text>
				<text class="value" :class="ticket.status">{{ getStatusText(ticket.status) }}</text>
			</view>
		</view>
		
		<view class="ticket-description">
			<text class="section-title">
				<text class="iconfont icon-description title-icon"></text>
				问题描述
			</text>
			<view class="description-content">
				<text>{{ ticket.description }}</text>
			</view>
		</view>
		
		<view class="ticket-process">
			<text class="section-title">
				<text class="iconfont icon-process title-icon"></text>
				处理进度
			</text>
			<view class="process-list">
				<view class="process-item" v-for="(record, index) in ticket.processRecords" :key="index">
					<view class="process-header">
						<text class="processor">{{ record.processor }}</text>
						<text class="process-time">{{ record.time }}</text>
					</view>
					<view class="process-content">
						<text>{{ record.content }}</text>
					</view>
				</view>
				
				<view class="no-records" v-if="!ticket.processRecords || ticket.processRecords.length === 0">
					<text class="iconfont icon-empty empty-icon"></text>
					<view>暂无处理记录</view>
				</view>
			</view>
		</view>
		
		<view class="action-buttons" v-if="ticket.status === 'processing'">
			<button class="action-btn cancel-btn" @click="cancelTicket">
				<text class="iconfont icon-cancel"></text>
				<text>撤销工单</text>
			</button>
		</view>
	</view>
</template>

<script>
	import { isLoggedIn } from '../../../utils/auth.js';
	
	export default {
		data() {
			return {
				ticket: {
					id: 'C20210901001',
					title: '网络连接异常',
					type: '网络问题',
					description: '办公室网络频繁断开连接，影响正常工作。已经尝试重启路由器但问题仍然存在。',
					status: 'processing',
					createTime: '2021-09-01 10:30',
					processRecords: [
						{
							processor: '技术支持',
							time: '2021-09-01 11:00',
							content: '已收到您的工单，正在分析问题原因。'
						},
						{
							processor: '网络工程师',
							time: '2021-09-01 14:30',
							content: '已安排现场检查，预计今天下午3点到达。'
						}
					]
				}
			}
		},
		onLoad(options) {
			// 检查用户是否已登录
			if (!isLoggedIn()) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				
				setTimeout(() => {
					uni.redirectTo({
						url: '/pages/login/login'
					});
				}, 1000);
				return;
			}
			
			// 实际项目中这里应该根据options.id从后端获取工单详情
			if (options.id) {
				console.log('获取工单详情，ID:', options.id);
			}
		},
		methods: {
			getStatusText(status) {
				const statusMap = {
					'processing': '处理中',
					'completed': '已完成'
				};
				return statusMap[status] || '未知';
			},
			
			cancelTicket() {
				uni.showModal({
					title: '确认撤销',
					content: '确定要撤销此工单吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showToast({
								title: '工单已撤销',
								icon: 'success'
							});
							
							// 更新工单状态
							this.ticket.status = 'completed';
							
							setTimeout(() => {
								uni.navigateBack();
							}, 1500);
						}
					}
				});
			}
		}
	}
</script>

<style scoped>
	.ticket-detail-container {
		padding: 20rpx;
	}
	
	.ticket-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: linear-gradient(135deg, #007AFF, #00BCFF);
		border-radius: 20rpx;
		padding: 40rpx;
		color: white;
		margin-bottom: 30rpx;
	}
	
	.ticket-title {
		font-size: 36rpx;
		font-weight: bold;
		flex: 1;
	}
	
	.ticket-status {
		font-size: 24rpx;
		padding: 10rpx 20rpx;
		border-radius: 20rpx;
	}
	
	.ticket-status.processing {
		background-color: #ffeee0;
		color: #ff6b00;
	}
	
	.ticket-status.completed {
		background-color: #e8f8f5;
		color: #00b894;
	}
	
	.ticket-info {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
	}
	
	.info-item {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}
	
	.info-item:last-child {
		margin-bottom: 0;
	}
	
	.info-icon {
		font-size: 36rpx;
		margin-right: 10rpx;
		color: #007AFF;
	}
	
	.label {
		font-size: 28rpx;
		color: #999;
		width: 180rpx;
	}
	
	.value {
		font-size: 28rpx;
		flex: 1;
	}
	
	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
	}
	
	.title-icon {
		font-size: 36rpx;
		margin-right: 10rpx;
		color: #007AFF;
	}
	
	.ticket-description {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
	}
	
	.description-content {
		font-size: 28rpx;
		line-height: 1.6;
		color: #666;
	}
	
	.ticket-process {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
	}
	
	.process-item {
		border-left: 2rpx solid #007AFF;
		padding-left: 30rpx;
		margin-bottom: 40rpx;
		position: relative;
	}
	
	.process-item:last-child {
		margin-bottom: 0;
	}
	
	.process-item::before {
		content: '';
		width: 20rpx;
		height: 20rpx;
		border-radius: 50%;
		background-color: #007AFF;
		position: absolute;
		left: -11rpx;
		top: 5rpx;
	}
	
	.process-header {
		display: flex;
		justify-content: space-between;
		margin-bottom: 10rpx;
	}
	
	.processor {
		font-size: 28rpx;
		font-weight: bold;
	}
	
	.process-time {
		font-size: 24rpx;
		color: #999;
	}
	
	.process-content {
		font-size: 28rpx;
		color: #666;
		line-height: 1.5;
	}
	
	.no-records {
		text-align: center;
		padding: 40rpx 0;
		color: #999;
	}
	
	.empty-icon {
		font-size: 100rpx;
		color: #ccc;
		margin-bottom: 20rpx;
		display: block;
	}
	
	.action-buttons {
		display: flex;
		justify-content: center;
		padding: 0 20rpx;
	}
	
	.action-btn {
		flex: 1;
		height: 80rpx;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		font-weight: bold;
	}
	
	.cancel-btn {
		background-color: #ff4d4f;
		color: white;
		border: none;
	}
	
	.action-btn .iconfont {
		margin-right: 10rpx;
		font-size: 36rpx;
	}
</style>